package com.itheima.servletContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//获取ServletContext对象
public class ServletDemo10 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        //1.获取全局初始化参数
        String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
        System.out.println(contextConfigLocation);
        //2.获取应用的访问虚拟目录
        String contextPath = servletContext.getContextPath();
        System.out.println(contextPath);
        //3.1 获取src目录下a.txt文件的磁盘绝对路径
        String realPath = servletContext.getRealPath("/WEB-INF/classer/a.txt");
        //3.2 获取web目录下b.txt文件的磁盘绝对路径
        String realPath1 = servletContext.getRealPath("/b.txt");
        //3.3 获取WEB-INF目录下c.txt文件的磁盘绝对路径
        String realPath2 = servletContext.getRealPath("/WEB-INF/c.txt");
        System.out.println(realPath);
        System.out.println(realPath1);
        System.out.println(realPath2);
        //4.1 获取域对象中存储的数据
        Object hello = servletContext.getAttribute("hello");
        System.out.println(hello);
        //4.2 移除域对象中username的数据
        servletContext.removeAttribute("hello");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
